package com.hsgene.consumables.controller;

import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.support.LimitHelper;
import com.hsgene.common.util.support.OrderHelper;
import com.hsgene.common.util.support.Pagination;
import com.hsgene.consumables.condition.GoodsCondition;
import com.hsgene.consumables.domain.ConsumablesInventoryInfo;
import com.hsgene.consumables.dto.ConsumablesInventoryInfoDto;
import com.hsgene.consumables.dto.peanut.GoodsKeywordsDto;
import com.hsgene.consumables.exception.ConsumablesErrorCode;
import com.hsgene.consumables.service.ConsumablesInventoryService;
import com.hsgene.product.domain.GeneticCommodityCategory;
import com.hsgene.product.exception.ProductErrorCode;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 耗材库存
 *
 * @author wxf
 * @date 2018/8/15 8:43
 **/
@RestController
@RequestMapping("/v1.0/genetic/testing/goods")
public class ConsumablesInventoryController {

    /**
     * Log
     */
    private static final Logger LOGGER = LogManager.getLogger(ConsumablesInventoryController.class);

    @Resource
    private ConsumablesInventoryService inventoryService;

    /**
     * 添加库存耗材信息
     *
     * @param inventoryInfo 库存信息
     * @return
     */
    @PostMapping
    public ApiResult<Integer> addInventoryInfo(@RequestBody ConsumablesInventoryInfo inventoryInfo) {
        int result = inventoryService.addInventoryInfo(inventoryInfo);
        return new ApiResult(200, "success", "添加成功", result);
    }

    /**
     * 查询耗材列表
     *
     * @return
     */
    @GetMapping
    public ApiResult queryInventoryInfo(Integer page
            ,Integer size,@RequestParam(value = "orderField" ,defaultValue = "update_date_time") String orderField
            ,GoodsCondition condition
            ,String isAsc
            ,@RequestParam(name = "range_type",required = false)String rangeType) {

            if(StringUtils.isNotEmpty(rangeType)&&"all".equals(rangeType)){
                return inventoryService.getInventoryInfoAll();
            }
            else{
                condition.setLimitHelper(LimitHelper.create(page, size, true));
                String field="g1."+orderField;
                boolean isA= Boolean.parseBoolean(isAsc);
                condition.setOrderHelper(OrderHelper.create(field, isA));
                Pagination<ConsumablesInventoryInfoDto> result = inventoryService.queryInventoryInfo(condition);
                return  ApiResult.succ(result);
            }
    }
    /**
     *根据耗材名称校验
     * @author FX
     * @date 2018/10/30 10:01
     * @param name
     * @param id
     * @return
     */
    @GetMapping("name")
    public ApiResult getByGoodsName(String name,String id){
        ApiResult<ConsumablesInventoryInfoDto> byName =inventoryService.getByGoodsName(name);
        if(StringUtils.isNotEmpty(id)){
            ApiResult<ConsumablesInventoryInfoDto> byId = inventoryService.getById(id);
            if(byId.getData().getName().equals(name)){
                return ApiResult.succ();
            }
        }
        if(byName.getData()==null){
            return ApiResult.succ();
        }
        return ApiResult.fail(ConsumablesErrorCode.CONSUMABLES_ALREADY_EXIST) ;
    }

    /**
     * 根据关键字搜索耗材(花生仁)
     * @param
     * @return
     */
    @GetMapping("keywords")
    public ApiResult<List<GoodsKeywordsDto>> getInventoryInfoByKeywords(String keyword) {
        return inventoryService.getInventoryInfoByKeywords(keyword);
    }

    /**
     * 查询所有耗材列表
     *
     * @return
     */
    @GetMapping("all")
    public ApiResult<List<ConsumablesInventoryInfoDto>> getInventoryInfoAll() {
        return inventoryService.getInventoryInfoAll();
    }


    /**
     * 删除耗材库存信息
     *
     * @param id id
     * @return
     */
    @DeleteMapping("{id}")
    public ApiResult<ConsumablesInventoryInfoDto> deleteInventoryInfo(@PathVariable("id") String id) {
            ApiResult<ConsumablesInventoryInfoDto> result = inventoryService.deleteInventoryInfo(id);
            return result;
    }

    /**
     * 批量删除耗材库存信息
     *
     * @param list
     * @return
     */
    @DeleteMapping
    public ApiResult<ConsumablesInventoryInfo> deleteInventoryInfoMore(@RequestBody List<ConsumablesInventoryInfo> list) {
        return inventoryService.deleteInventoryInfoMore(list);
    }

    /**
     * 获取详情
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ApiResult<ConsumablesInventoryInfoDto> getInventoryDetais(@PathVariable("id") String id) {
            ConsumablesInventoryInfoDto result = inventoryService.getInventoryDetais(id);
            return new ApiResult(200, "success", "查询成功", result);
    }
    /**
     * 获取详情
     *
     * @param
     * @return
     */
    @GetMapping("ids")
    public ApiResult<List<ConsumablesInventoryInfoDto>> getInventoryDetaisByIds(@RequestParam("ids") String ids) {
           return inventoryService.getInventoryDetaisByIds(ids);
    }

    /**
     * 修改信息
     *
     * @param inventoryInfo
     * @return
     */
    @PutMapping
    public ApiResult<String> updateInventoryInfo(@RequestBody ConsumablesInventoryInfo inventoryInfo) {
            boolean result = inventoryService.updateInventoryInfo(inventoryInfo);
            //false说明数据已存在
            if (!result) {
                return ApiResult.fail(ConsumablesErrorCode.UPDATE_IS_EXISTS);
            }
            return new ApiResult(200, "success", "修改成功", String.valueOf(result));
    }
}
